<?php
class News_IndexController extends Crb_Controller
{
	
   public function preDispatch()
   {
        parent::preDispatch();
        $this->view->Title = 'Новости';
   }
    

   public function indexAction()
    {
        $News = new News();
        $this->view->News = $News->getAll();
        
    }

    public function viewAction()
    {
        $ID = $this->_request->getParam('id',0);
        $News = new News();
        $oneNews = $News->getOne($ID);
        $this->view->Row = $oneNews; 
        $this->view->Title = $oneNews['Title']; 
    }
    
    

   	public function listAction()
	{
		$this->view->layout()->setLayout('admin');
		$News = new News();
		$News = $News->getAll();
		$this->view->News = $News;
		$this->view->Title = "Список новостей"; 
	}
	
    public function editAction()
    {
    	$this->view->layout()->setLayout('admin');
    	$ID = $this->_request->getParam('id',0);
        $News = new News();
        $Row = $News->getOne($ID);
        
        $this->view->Title = "Редактирование новости";
        
        if($this->_request->isPost())
        {
        	$Title = $this->_request->getParam('title','');
        	$Text = $this->_request->getParam('text','');
        	$Date = $this->_request->getParam('date','');
        	$Teaser = $this->_request->getParam('teaser','');
        	
        	$NewFileName = "";
        	if (isset($_FILES['photo']) && !empty($_FILES['photo']['tmp_name']))
        	{
        		
	            $ImagesPath     = ROOT . DIRECTORY_SEPARATOR . 'images';
	            //$ImagesPath     = 'c:/tmp/images';
	
	            $FullImagePath  = $ImagesPath . DIRECTORY_SEPARATOR . 'full';
	            $SmallImagePath = $ImagesPath . DIRECTORY_SEPARATOR . 'thumb_small';
	            $BigImagePath   = $ImagesPath . DIRECTORY_SEPARATOR . 'thumb_big';
	             
            
        		$Photo = $_FILES['photo'];
        		
        		$NewFileName = md5(time() . $Photo['tmp_name']) . ".jpg";

        		copy($Photo['tmp_name'],$FullImagePath . DIRECTORY_SEPARATOR . $NewFileName);
        		$ImageProc = new Image();
//        		$ImageProc->smartResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,180,70,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
                    $ImageProc->smartResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,300,300,$BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);
        		//                    $ImageProc->hardResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,139,80,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
      		                    $ImageProc->cropResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,180,70,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
        	}
        	 
        	
        	$News->Edit($ID,$Title,$Text,$Date);
        	$News->setPressRelise($ID, $this->_request->getParam('PressRelise',0));
        	$News->setTeaser($ID, $Teaser);
        	
        	if (!empty($NewFileName))$News->setPicture($ID,$NewFileName);
        	$this->_redirect('news/list');
        }
        $this->view->Row = $Row;
    }
    
    
    
    public function addAction()
    {
        $this->view->layout()->setLayout('admin');
    	if($this->_request->isPost())
        {
            $Title = $this->_request->getParam('title','');
            $Text  = nl2br($this->_request->getParam('text',''));
            $Date  = $this->_request->getParam('date','');
            $Teaser = $this->_request->getParam('teaser','');
            
            //  add picture upload
            
            $NewFileName = "";
            $ImagesPath     = ROOT . DIRECTORY_SEPARATOR . 'images';
            //$ImagesPath     = 'c:/tmp/images';

            $FullImagePath  = $ImagesPath . DIRECTORY_SEPARATOR . 'full';
            $SmallImagePath = $ImagesPath . DIRECTORY_SEPARATOR . 'thumb_small';
            $BigImagePath   = $ImagesPath . DIRECTORY_SEPARATOR . 'thumb_big';
             
              if (isset($_FILES['photo']) && !empty($_FILES['photo']['tmp_name']))
                {
                    $Photo = $_FILES['photo'];
                    $NewFileName = md5(time() . $Photo['tmp_name']) . ".jpg";

                    copy($Photo['tmp_name'],$FullImagePath . DIRECTORY_SEPARATOR . $NewFileName);
                    $ImageProc = new Image();
                    $ImageProc->smartResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,300,300,$BigImagePath . DIRECTORY_SEPARATOR . $NewFileName);
      		        $ImageProc->cropResize($FullImagePath . DIRECTORY_SEPARATOR . $NewFileName,180,70,$SmallImagePath . DIRECTORY_SEPARATOR . $NewFileName);
      		        
                }
            
            $News = new News();
            $NewID = $News->Add($Title,$Text,$Date);
            $News->setTeaser($NewID, $Teaser);
            $News->setPressRelise($NewID, $this->_request->getParam('PressRelise',0));
            
            if (!empty($NewFileName))$News->setPicture($NewID,$NewFileName);
            
            $this->_redirect('news/list');
        }
        
    }
    
    public function removeAction()
    {
    	$ID = $this->_request->getParam('id',0);
    	$News = new News();
    	$News->Remove($ID);
    	$this->_redirect('news/list');
    } 
    
    
	
}